class_name Big extends Node2D
# 这是普通大子类，同时也所有转型体的基类

var pre_jian = preload("res://游戏/core/大子/键线/键.tscn")
var pre_state = preload("res://游戏/core/大子/状态/BigState.tscn")

var scale_inPoint = Vector2(0.18,0.18)			# 在point上时，缩放比例
var player:Player
var point:Point
var actionTime:int = 0
var id:String

var mother_small:Small
var father_big:Big
var jian:Jian
var arr_states:Array = []
var evolution:String = "未转型"		#转型
var view_area = 1					#视野范围 

func _ready():
	visible = false
	arr_states.append(BigStateType.HIDE)
	$"放大版".scale = Vector2(0,0)

# 出大时执行
func init(data:Dictionary,pointt:Point):
	var gameManager = ObjectManager.gameManager
	id = data.id
	player = gameManager.map_players.get(data.player_username)
	player.map_bigs.merge({id:self})
	mother_small = player.small
	father_big = player.map_bigs.get(data.father_id)
	actionTime = 0
	self.position = pointt.position
	self.point = pointt
	self.point.big = self
	$"放大版/外框".self_modulate = player.color
	$"缩小版/大子图片".self_modulate = player.color
	$"缩小版".scale = scale_inPoint
	$"缩小版/可行动提示点".visible = false
	$"缩小版/可行动提示点2".visible = false
	$"放大版/可行动提示点".visible = false
	$"放大版/可行动提示点2".visible = false
	createJian()
	initState()

# 全部转型时执行，大子数据保留，特殊情况子类自己处理
func evolve(old:Big):
	player = old.player
	point = old.point
	id = old.id
	mother_small = old.mother_small
	father_big = old.father_big
	player.map_bigs.merge({id:self},true)
	position = point.position
	point.big = self
	$"放大版/外框".self_modulate = player.color
	$"缩小版/大子图片".self_modulate = player.color
	$"缩小版".scale = scale_inPoint
	$"缩小版/可行动提示点".visible = actionTime > 0
	$"缩小版/可行动提示点2".visible = actionTime > 1
	$"放大版/可行动提示点".visible = actionTime > 0
	$"放大版/可行动提示点2".visible = actionTime > 1
	createJian()
	if ObjectManager.gameManager.me_player == null || player.is_me:
		ObjectManager.soundManager.play(SoundType.big_evolve)

func on_mouse_entered():
	$"缩小版/缩小版动画".play("鼠标进入")
	
func on_mouse_exited():
	$"缩小版/缩小版动画".play("鼠标移出")
	
func click():
	$"放大版/放大版动画".play("点击放大")
	
func detailHide():
	$"放大版/放大版动画".play("缩小隐藏")

func _on_animation_started(anim_name):
	if anim_name == "点击放大":
		ObjectManager.gameManager.layer_chesses.remove_child(self)
		ObjectManager.gameManager.layer_display.add_child(self)

func _on_animation_finished(anim_name):
	if anim_name == "缩小隐藏":
		ObjectManager.gameManager.layer_display.remove_child(self)
		ObjectManager.gameManager.layer_chesses.add_child(self)

func update(data:Dictionary):
	if player.is_me:
		actionTime = data.actionTime
		$"缩小版/可行动提示点".visible = actionTime > 0
		$"缩小版/可行动提示点2".visible = actionTime > 1
		$"放大版/可行动提示点".visible = actionTime > 0
		$"放大版/可行动提示点2".visible = actionTime > 1
	updateState(data.arr_states)

func createJian():
	jian = pre_jian.instantiate()
	ObjectManager.gameManager.layer_jian.add_child(jian)

func updataJian():
	var end_position:Vector2
	if father_big != null:
		end_position = father_big.point.position
	else :
		if !mother_small:
			return
		end_position = mother_small.point.position
	var distance = point.position.distance_to(end_position)
	var dx = end_position.x - point.position.x
	var angle = acos(dx/distance)
	if end_position.y < point.position.y:
		angle *= -1
	jian.init(point.position,player.color,distance,angle)

# 全部大子显示判定
func updateVisible():
	# 判断显示不显示
	if player.is_me || player.is_beSmelling || !ObjectManager.gameManager.me_player || !ObjectManager.gameManager.me_player.alive:
		visible = true
		point.changeColor(player.color)
	else :
		if point.is_view:
			if evolution == "未转型" && arr_states.has(BigStateType.HIDE):
				visible = false
			else :
				visible = true
			# point外圈颜色刷新
			if visible:
				point.changeColor(player.color)
			else:
				point.changeColor(Color.DARK_GRAY)
		else:
			visible = false

# 全部大子显示判定后才会进行全部键显示判定
func updateJianVisible():
	if father_big!=null:
		jian.visible = visible && father_big.visible
	else :
		jian.visible = visible
	updataJian()

# 如果大子移动后进入潜行状态，由于刷新不及时会有一闪的情况，先给他设置成看不到的
func moveBefore():
	#如果第二个玩家正在查看该大子，先把放大版取消掉
	if !player.is_me:
		var gameManager = ObjectManager.gameManager as GameManager
		var operation = gameManager.operationManager
		if operation.selectedPoint == point:
			var ui = gameManager.ui as UI
			ui._on_取消_pressed()
	#先把自己潜行了，不然会闪一下
	visible = false
	jian.visible = false
	arr_states.append(BigStateType.HIDE)
	point.changeColor(Color.BLACK)


# 有些特殊大子死前会干点什么
func dieBefore():
	pass

# 大子死亡
func die():
	dieBefore()
	queue_free()
	jian.queue_free()
	# 如果大子正在操作中，点击一下取消
	var gameManager = ObjectManager.gameManager as GameManager
	var operationManager = gameManager.operationManager as OperationManager
	var selectedPoint = operationManager.selectedPoint as Point
	if selectedPoint == point:
		gameManager.ui.closeAll()

# 被附身，重新刷新
func possess():
	$"放大版/外框".self_modulate = player.color
	$"缩小版/大子图片".self_modulate = player.color

func initState():
	$"放大版/状态容器/潜行".init(BigStateType.HIDE)
	$"放大版/状态容器/护盾".init(BigStateType.SHIELD)
	$"放大版/状态容器/怯战".init(BigStateType.FEAR)
	$"放大版/状态容器/冰冻".init(BigStateType.ICE)
	$"放大版/状态容器/焦渴".init(BigStateType.BURDEN)
	$"放大版/状态容器/中毒".init(BigStateType.METHYSIS)
	$"放大版/状态容器/嗜血".init(BigStateType.BLOODTHIRSTY)
	$"放大版/状态容器/踏浪".init(BigStateType.WAVERIDER)
	$"放大版/状态容器/旺育".init(BigStateType.FERTILE)
	$"放大版/状态容器/常胜".init(BigStateType.HEROIC)
	$"缩小版/状态合集/护盾".visible = false
	$"缩小版/状态合集/中毒".visible = false
	$"缩小版/状态合集/冰冻".visible = false
	$"缩小版/状态合集/潜行".visible = false
	$"缩小版/状态合集/焦渴".visible = false
	$"缩小版/状态合集/怯战".visible = false
	$"缩小版/状态合集/嗜血".visible = false
	$"缩小版/状态合集/踏浪".visible = false
	$"缩小版/状态合集/旺育".visible = false
	$"缩小版/状态合集/常胜".visible = false
	$"放大版/状态容器/潜行".visible = false
	$"放大版/状态容器/护盾".visible = false
	$"放大版/状态容器/怯战".visible = false
	$"放大版/状态容器/冰冻".visible = false
	$"放大版/状态容器/焦渴".visible = false
	$"放大版/状态容器/中毒".visible = false
	$"放大版/状态容器/嗜血".visible = false
	$"放大版/状态容器/踏浪".visible = false
	$"放大版/状态容器/旺育".visible = false
	$"放大版/状态容器/常胜".visible = false


# 状态更新
func updateState(data_arr_states: Array):
	if evolution != "未转型":
		return
	arr_states = data_arr_states
	$"缩小版/状态合集/护盾".visible = false
	$"缩小版/状态合集/中毒".visible = false
	$"缩小版/状态合集/冰冻".visible = false
	$"缩小版/状态合集/潜行".visible = false
	$"缩小版/状态合集/焦渴".visible = false
	$"缩小版/状态合集/怯战".visible = false
	$"缩小版/状态合集/嗜血".visible = false
	$"缩小版/状态合集/踏浪".visible = false
	$"缩小版/状态合集/旺育".visible = false
	$"缩小版/状态合集/常胜".visible = false
	$"放大版/状态容器/潜行".visible = false
	$"放大版/状态容器/护盾".visible = false
	$"放大版/状态容器/怯战".visible = false
	$"放大版/状态容器/冰冻".visible = false
	$"放大版/状态容器/焦渴".visible = false
	$"放大版/状态容器/中毒".visible = false
	$"放大版/状态容器/嗜血".visible = false
	$"放大版/状态容器/踏浪".visible = false
	$"放大版/状态容器/旺育".visible = false
	$"放大版/状态容器/常胜".visible = false
	for state in arr_states:
		match state:
			BigStateType.HIDE:
				$"缩小版/状态合集/潜行".visible = true
				$"放大版/状态容器/潜行".visible = true
			BigStateType.SHIELD:
				$"缩小版/状态合集/护盾".visible = true
				$"放大版/状态容器/护盾".visible = true
			BigStateType.FEAR:
				$"缩小版/状态合集/怯战".visible = true
				$"放大版/状态容器/怯战".visible = true
			BigStateType.ICE:
				$"缩小版/状态合集/冰冻".visible = true
				$"放大版/状态容器/冰冻".visible = true
			BigStateType.BURDEN:
				$"缩小版/状态合集/焦渴".visible = true
				$"放大版/状态容器/焦渴".visible = true	
			BigStateType.METHYSIS_BEFORE,BigStateType.METHYSIS:
				$"缩小版/状态合集/中毒".visible = true
				$"放大版/状态容器/中毒".visible = true
			BigStateType.BLOODTHIRSTY:
				$"缩小版/状态合集/嗜血".visible = true
				$"放大版/状态容器/嗜血".visible = true
			BigStateType.WAVERIDER:
				$"缩小版/状态合集/踏浪".visible = true
				$"放大版/状态容器/踏浪".visible = true
			BigStateType.FERTILE:
				$"缩小版/状态合集/旺育".visible = true
				$"放大版/状态容器/旺育".visible = true
			BigStateType.HEROIC:
				$"缩小版/状态合集/常胜".visible = true
				$"放大版/状态容器/常胜".visible = true
	$"缩小版/状态合集/状态轮播动画器".update(arr_states)

# 有些转型体要用
func turnStart():
	pass
